<?php

class Pages_Widgets_Categories_Widget extends Nimblecms_Widget 
{
	protected function _prepareShow() 
	{
		$request = Zend_Controller_Front::getInstance()->getRequest();
		$slug = $request->getParam('slug', 0);
		
		$tree = array();
		if($slug) {
			$mapper = Doctrine_Core::getTable('Pages_Model_Page');
			$page = $mapper->findOneByAlias($slug);
			if($page) {
				$root_id = $page->getNode()->getRootValue();
				
				$treeObject = $mapper->getTree();
				
				$options = array('root_id' => $root_id);
				
				foreach ($treeObject->fetchTree($options) as $key=>$node) {
					if($node->level > 0) {
						$tree[$key]['level'] = $node->level -1;
						$tree[$key]['id'] = $node->id;
						$tree[$key]['alias'] = $node->alias;
						$tree[$key]['title'] = $node->Translation['ru']->title;
					}
				}
			}
		}
		
		$this->_view->tree  = $tree;
	}
}
